import RequestSuccessCallbackResult = WechatMiniprogram.RequestSuccessCallbackResult
import ReadFileSuccessCallbackResult = WechatMiniprogram.ReadFileSuccessCallbackResult
import { Video } from './video'
import { isUrl, getFileByRequest, getFileByManager } from '../Utils/index'

const { inflate } = require('pako')
const { ProtoMovieEntity } = require('./proto')


export class Parser {
  // 加载svga资源
  init(url: string): Promise<Video> {
    return new Promise(async (resolve, reject) => {
      try {
        let result: RequestSuccessCallbackResult | ReadFileSuccessCallbackResult = isUrl(url) ? await getFileByRequest(url) : await getFileByManager(url)
        const inflateData = inflate(result.data)
        resolve(new Video(ProtoMovieEntity.decode(inflateData)))
      } catch (err) {
        reject(err)
      }
    })
  }
}